<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/4 0004
 * Time: 12:00
 */

namespace app\admin\controller;


use OSS\Core\OssException;
use OSS\OssClient;

class Upload extends Base
{
    public function ajax_upload_img()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');

        if(empty($file)){
            echo api_return(201,'未收到上传图片');
            exit;
        }
//        $path = ROOT_PATH . 'public' .DS.'images'.DS.'product';

        $info = $this->upload();
        if($info['code'] == 1){
            echo api_return(200,'上传成功',['img_url'=>$info['data'],'name'=>$info['name']]);
        }else{
            echo api_return(203,$info['data']);
        }
    }

    private function upload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');

        $path = ROOT_PATH.'public'.DS.'images';
        $ext = config('setting.upload_file_ext');
        $max = config('setting.upload_file_max');
        $info = $file->validate(['size'=>$max*1024,'ext'=>$ext])->move($path);

        if($info){
            $savename = $info->getSaveName();
            //网页
            $data = DS.'images'.DS.$savename;

            //小程序
//            $domain = request()->domain();
//            $data = $domain.'/public/images/'.$savename;

            $data = str_replace("\\", "/", $data);

            $back['code'] = 1;
            $back['data'] = $data;
            $back['name'] = '';
        }else{
            // 上传失败获取错误信息
            $back['code'] = 0;
            $back['data'] = $file->getError();
        }
        return $back;
    }

    //oss上传
    private function upload1()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');
        $info = $file->getInfo();

        $extension = explode('.', $info['name'])[1];

        $ext = config('setting.upload_file_ext');
        $max = config('setting.upload_file_max');
        $access = $file->check(['size'=>$max*1000,'ext'=>$ext]);

        if($access){

            $fileName = md5(time()).'.'.$extension;
//            dump($file->getPathname());
//            exit;
            $res = $this->oss($fileName,$info['tmp_name']);
            $back['code'] = 1;
            $back['data'] = $res['info']['url'];
            $back['name'] = '';
        }else{
            // 上传失败获取错误信息
            $back['code'] = 0;
            $back['data'] = $file->getError();

        }

        return $back;
    }

    public function oss($fileName,$path)
    {
        $config = config('wechat.oss');
        try {
            $ossClient = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint']);
        } catch (OssException $e) {
            trace("oss:".$e->getMessage(),'error');
            return false;
        }

        //文件名称
        $object = "";
        //内容
//        $content = "Hello, OSS!"; // Content of the uploaded file
        try {
            $res = $ossClient->uploadFile($config['bucket'], $fileName, $path);
            return $res;
        } catch (OssException $e) {
            trace("oss:".$e->getMessage(),'error');
            return false;
        }
    }
}